﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace envir2plotwatt.GUI.Configuration
{
    public partial class AddEditMonitorMapping : Form
    {
        private GetMonitorListDelegate m_getMonitorList;
        public GetMonitorListDelegate GetMonitorList
        {
            get { return m_getMonitorList; }
            set { m_getMonitorList = value; }
        }

        private GetMeterListDelegate m_getMeterList;
        public GetMeterListDelegate GetMeterList
        {
            get { return m_getMeterList; }
            set { m_getMeterList = value; }
        }

        private FormMode m_mode;
        private MonitorToMeter m_mapping;

        public MonitorToMeter Mapping
        {
            get { return m_mapping; }
        }

        public enum FormMode
        {
            Add,
            Edit
        }

        public AddEditMonitorMapping(FormMode mode, MonitorToMeter mapping = null)
        {
            InitializeComponent();

            m_mode = mode;
            m_mapping = mapping;
        }

        private void ConfigureFormMode()
        {
            switch (m_mode)
            {
                case FormMode.Add:
                    m_mapping = new MonitorToMeter();
                    btnAction.Text = "Add";
                    break;

                case FormMode.Edit:
                    cbMeter.Text = m_mapping.Meter;
                    cbMonitor.Text = m_mapping.Monitor;
                    btnAction.Text = "Edit";
                    break;
            }
            ValidateEntry();
        }

        private void btnAction_Click(object sender, EventArgs e)
        {
            m_mapping.Meter = cbMeter.Text;
            m_mapping.Monitor = cbMonitor.Text;

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void AddEditMonitorMapping_Load(object sender, EventArgs e)
        {
            foreach (string str in GetMonitorList())
            {
                cbMonitor.Items.Add(str);
            }
            foreach (string str in GetMeterList())
            {
                cbMeter.Items.Add(str);
            }

            ConfigureFormMode();
        }

        private void cbMeter_SelectedValueChanged(object sender, EventArgs e)
        {
            ValidateEntry();
        }

        private void cbMonitor_SelectedValueChanged(object sender, EventArgs e)
        {
            ValidateEntry();
        }

        private void ValidateEntry()
        {
            if (string.IsNullOrWhiteSpace(cbMeter.Text) || string.IsNullOrWhiteSpace(cbMonitor.Text))
            {
                btnAction.Enabled = false;
            }
            else
            {
                btnAction.Enabled = true;
            }
        }
    }
}
